/**
 * Created by tdzl2003 on 12/18/16.
 */
import React, {Component} from "react";
import {
    Image,
    NativeModules,
    StyleSheet,
    Text,
    TouchableOpacity,
    View,
    Platform,
    DeviceEventEmitter,
} from 'react-native';
import ScrollableTabView, {DefaultTabBar} from 'react-native-scrollable-tab-view';
import SuggestList from './components/SuggestList';
import SuggestFindRequest from '../contract/suggest/request/SuggestFindRequest';

import agreeClose from './agreeClose';
import suggestDetail from './suggestDetail';
import suggestClose from './suggestClose';
import suggestHandle from './suggestHandle';
import ExamineSuggest from './ExamineSuggest';
import {hasAnyFunction } from '../utils/utils';
import CommStatusBar from './components/CommStatusBar';

import Add from './Add';

import RString from '../language/RString';
import RConfig from '../language/RConfig';

const styles = StyleSheet.create({
    container: {
        flex: 1,
        backgroundColor: 'white',
    },
    addSuggest: {
        marginRight: 10,
        color: '#fff',
        fontSize: 28,
    },
    commonHeader: {
        height: Platform.OS === 'android' ? 50 : 60,
        display: 'flex',
        backgroundColor: '#333238',
        flexDirection: 'row',
        alignItems: 'center',
        justifyContent: 'space-between',
        position: 'relative',
        paddingTop: Platform.OS === 'ios' ? 20 : 0,
    },
    back: {
        width: 30,
        left: 20,
        height: Platform.OS === 'android' ? 50 : 60,
        justifyContent: 'center',
    },
    toAdd: {
        width: 30,
        right: 10,
        height: Platform.OS === 'android' ? 50 : 60,
        justifyContent: 'center',
    },
    languageBox: {
        width: 35,
        justifyContent: 'center',
        height: 35,
        display: 'flex',
        alignItems: 'center',
        backgroundColor: '#3499ff',
        borderRadius: 35,
        position: 'absolute',
        bottom: 40,
        right: 30,
    },
    language: {
        fontSize: 14,
        color: '#fff',
        fontWeight: 'bold',
    },
});

export default class MySuggestList extends Component {
    constructor(props) {
        super(props);

        // 已关闭
        const closeRequest = new SuggestFindRequest();
        closeRequest.setPageSize(0);
        closeRequest.setStatus("CLOSED");
        closeRequest.setRelativeUserId(global.userId);
        // 待分配
        const toacceptRequest = new SuggestFindRequest();
        toacceptRequest.setPageSize(0);
        toacceptRequest.setStatus("TO_ACCEPT");
        toacceptRequest.setRelativeUserId(global.userId);
        // 待处理
        const todealRequest = new SuggestFindRequest();
        todealRequest.setPageSize(0);
        todealRequest.setStatus("TO_DEAL");
        todealRequest.setRelativeUserId(global.userId);
        // 待关闭
        const tocloseRequest = new SuggestFindRequest();
        tocloseRequest.setPageSize(0);
        tocloseRequest.setStatus("TO_CLOSE");
        tocloseRequest.setRelativeUserId(global.userId);
        // 待审核
        const examineRequest = new SuggestFindRequest();
        examineRequest.setPageSize(0);
        examineRequest.setStatus("TO_EXAMINE");
        examineRequest.setRelativeUserId(global.userId);
        // 查看
        const viewSuggestRequest = new SuggestFindRequest();
        viewSuggestRequest.setPageSize(0);
        viewSuggestRequest.setType("ALL")
        viewSuggestRequest.setRelativeUserId(global.userId);
        this.state = {
            closeRequest: closeRequest,
            toacceptRequest: toacceptRequest,
            todealRequest: todealRequest,
            tocloseRequest: tocloseRequest,
            examineRequest: examineRequest,
            viewSuggestRequest: viewSuggestRequest,
            AorB: true,//代表当前显示是中文，按钮显示EN
            closed: hasAnyFunction("api.suggest.closed"),
            create: hasAnyFunction("api.suggest.create"),
            toview: hasAnyFunction("api.suggest.toview"),
            toexamine: hasAnyFunction("api.suggest.toexamine"),
            todeal: hasAnyFunction("api.suggest.todeal"),
            toclosed: hasAnyFunction("api.suggest.toclosed"),
            todistribute: hasAnyFunction("api.suggest.todistribute"),
        };
    }

    componentWillMount() {
        if (RConfig.language == "zh") {
            this.setState({
                AorB: true
            });
        } else {
            this.setState({
                AorB: false
            });
        }
    }

    doClose = () => {
        NativeModules.system.navTo("BACK");
    };
    doAdd = () => {
        const {navigator} = this.props;
        if (navigator) {
            navigator.push({
                name: "Add",
                component: Add
            });
        }
    };

    changeLanguage = () => {
        if (RConfig.language == "zh") {
            RConfig.language = "en";
            this.setState({
                AorB: false
            });
        } else {
            RConfig.language = "zh";
            this.setState({
                AorB: true
            });
        }
        DeviceEventEmitter.emit("language", RConfig.language);
    };

    renderTabBar = () => {
        return (
            <DefaultTabBar
                activeTextColor="#3399ff"
                inactiveTextColor="#333333"
                textStyle={{fontSize: 15}}
                backgroundColor='#ffffff'
                underlineStyle={{backgroundColor: '#3399ff', height: 2}}
                tabStyle={{paddingBottom: 0}}
                style={{height: 50, marginTop: 1}}/>
        );
    };

    click = (item) => {
        const {navigator} = this.props;
        //待处理
        if (item.status == 'TO_DEAL') {/* && item.dealId == global.userId*/
            navigator.push({name: "suggestClose", component: suggestClose, params: {id: item.id}});
        }//待关闭
        else if (item.status == 'TO_CLOSE') {/*,&& item.suggestPersonId == global.userId*/
            navigator.push({name: "agreeClose", component: agreeClose, params: {id: item.id}});
        }//待审核
        else if (item.status == 'TO_EXAMINE') {/* && item.suggestPersonId == global.userId*/
            navigator.push({name: "ExamineSuggest", component: ExamineSuggest, params: {id: item.id}});
        }//待分配
        else if (item.status == 'TO_ACCEPT') {/*&& item.suggestPersonId == global.userId*/
            navigator.push({name: "suggestHandle", component: suggestHandle, params: {id: item.id}});
        }//查看
        else {
            navigator.push({name: "suggestDetail", component: suggestDetail, params: {id: item.id}});
        }
    };

    onChangeTab = (obj) => {
        console.log('index:' + obj);
    };

    render() {
        const {
            create,
            closed,
            toclosed,
            todistribute,
            todeal,
            toexamine,
            toview,
        } = this.state;
        return (
            <View style={styles.container}>
                <CommStatusBar/>
                <View style={styles.commonHeader}>
                    <TouchableOpacity onPress={this.doClose.bind(this)} style={styles.back}>
                        <Image source={require('./img/chahao.png')}/>
                    </TouchableOpacity>
                    <View style={{justifyContent: 'center'}}>
                        <Text style={{color: '#fff', fontSize: 16}}>{RString('suggestHome')}</Text>
                    </View>
                    {create ? <TouchableOpacity onPress={this.doAdd.bind(this)} style={styles.toAdd}>
                        <Image source={require('./img/add.png')}/>
                    </TouchableOpacity> : <Text />}
                </View>
                {(!closed && !toclosed && !todistribute && !todeal && !toexamine) ? null : (
                    <ScrollableTabView
                        style={{flex: 1}}
                        initialPage={0}
                        renderTabBar={this.renderTabBar}
                        onChangeTab={this.onChangeTab}
                    >
                        {
                            todistribute ? (
                                <SuggestList
                                    tabLabel={RString('toaccept')}
                                    language={RConfig.language}
                                    request={this.state.toacceptRequest}
                                    ref="todistributeRef"
                                    onClick={this.click}
                                />
                            ) : null
                        }
                        {
                            todeal ? (
                                <SuggestList
                                    tabLabel={RString('todeal')}
                                    language={RConfig.language}
                                    request={this.state.todealRequest}
                                    ref="todealRef"
                                    onClick={this.click}
                                />
                            ) : null
                        }
                        {
                            toexamine ? (
                                <SuggestList
                                    tabLabel={RString('toexamine')}
                                    language={RConfig.language}
                                    request={this.state.examineRequest}
                                    ref="toexamineRef"
                                    onClick={this.click}
                                />
                            ) : null
                        }
                        {
                            toclosed ? (
                                <SuggestList
                                    tabLabel={RString('toclose')}
                                    language={RConfig.language}
                                    request={this.state.tocloseRequest}
                                    ref="toclosedRef"
                                    onClick={this.click}
                                />
                            ) : null
                        }
                        {
                            closed ? (
                                <SuggestList
                                    tabLabel={RString('closed')}
                                    request={this.state.closeRequest}
                                    ref="closedRef"
                                    onClick={this.click}
                                />
                            ) : null
                        }
                        {
                            toview ? (
                                <SuggestList
                                    tabLabel={RString('toview')}
                                    language={RConfig.language}
                                    request={this.state.viewSuggestRequest}
                                    ref="toviewRef"
                                    onClick={this.click}
                                />
                            ) : null}
                    </ScrollableTabView>
                )}
                <TouchableOpacity
                    style={styles.languageBox}
                    onPress={this.changeLanguage}
                >
                    {this.state.AorB && <Text style={styles.language}>EN</Text>}
                    {!this.state.AorB && <Text style={styles.language}>中</Text>}
                </TouchableOpacity>
            </View>
        );
    }
}

